package com.nciae.controller;


import com.nciae.po.*;
import com.nciae.service.IDormitoryHeadService;
import com.nciae.service.IDormitoryService;
import com.nciae.service.IRectificationService;
import com.nciae.service.ISecurityQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.session.RedisSessionProperties;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Guoliyu
 * @since 2022-03-30
 */
@RestController
@RequestMapping("/dormitory")
public class DormitoryController {

    @Autowired
    private IDormitoryService dormitoryService;

    @Autowired
    private IDormitoryHeadService dormitoryHeadService;

    @Autowired
    private IRectificationService rectificationService;


    @PostMapping
    public ResponseEntity<Boolean> add(@RequestBody Dormitory dormitory) {
        dormitoryService.add(dormitory);
        return ResponseEntity.ok(true);
    }


    @GetMapping("/head/{id}")
    public ResponseEntity<List<Dormitory>> find(@PathVariable("id")Long id) {
        List<Dormitory> dormitories = dormitoryService.findDormitoryByHeadId(id);
        for(Dormitory dormitory : dormitories) {
            DormitoryHead dormitoryHead = dormitoryHeadService.findDormitoryById(dormitory.getDermitoryHeadId());
            if (!Objects.isNull(dormitoryHead)) {
                dormitory.setDermitoryHeadName(dormitoryHead.getUser().getUsername());
            }
            dormitory.setConnection(dormitory.getBuildingNo() + "-" + dormitory.getRoomNo());
        }
        return ResponseEntity.ok(dormitories);
    }

    /**
     * 返回宿舍长列表
     * @return
     */
    @GetMapping
    public ResponseEntity<List<Dormitory>> findAll() {
        List<Dormitory> dormitories = dormitoryService.findAll();
        for(Dormitory dormitory : dormitories) {
            DormitoryHead dormitoryHead = dormitoryHeadService.findDormitoryById(dormitory.getDermitoryHeadId());
            if (!Objects.isNull(dormitoryHead)) {
                dormitory.setDermitoryHeadName(dormitoryHead.getUser().getUsername());
            }
            dormitory.setConnection(dormitory.getBuildingNo() + "-" + dormitory.getRoomNo());
        }
        return ResponseEntity.ok(dormitories);
    }

    @GetMapping("/seurity/{id}")
    public ResponseEntity<List<SecurityQuestion>> findS(@PathVariable("id")Long id) {
        return ResponseEntity.ok(dormitoryService.findSecurityByDormitoryId(id));
    }

    @GetMapping("/score/{id}")
    public ResponseEntity<List<Score>> findT(@PathVariable("id")Long id) {
        return ResponseEntity.ok(dormitoryService.findScoreById(id));
    }

    @PutMapping
    public ResponseEntity<Boolean> update(@RequestBody Dormitory dormitory) {
        System.out.println(dormitory);
        dormitoryService.updateById(dormitory);
        return ResponseEntity.ok(true);
    }

}
